<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            height: 3000px;
        }
        .header{
            width: 100%;
            line-height: 80px;
            text-align: center;
            font-size: 30px;
            color: #fff;
            background-color: lightblue;
            position: fixed;
            top: -80px;
            left: 0;
            transition: top .5s linear;
        }

        .gotop{
            width: 50px;
            height: 50px;
            background-color: pink;
            font-size: 20px;
            text-align: center;
            color: #fff;
            position: fixed;
            bottom: 50px;
            right: 60px;
            opacity: 0;
        }

    </style>
</head>
<body>
   <div class="header">顶部导航</div> 
   <div class="gotop">回到顶部</div>
   <script>
       var header = document.querySelector('.header');
       var gotop = document.querySelector('.gotop');
       window.onscroll = function(){
           var height = document.documentElement.scrollTop;
           if(height>=300){
               header.style.top='0px';
               gotop.style.opacity = '1';

           }
           else{
                header.style.top='-80px';
                gotop.style.opacity = '0';
           }
       }
       gotop.onclick = function(){
           window.scrollTo({
               top:0,
               behavior:'smooth'
           })
       }
   </script>
</body>
</html>